package com.novax.ex.activity.provider.controller;

import com.novax.ex.activity.infrastructure.entity.RankRule;
import com.novax.ex.activity.infrastructure.entity.RankSubRule;
import com.novax.ex.activity.open.api.RankRuleApi;
import com.novax.ex.activity.provider.service.RankRuleService;
import com.novax.ex.activity.provider.service.RankSubRuleService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Optional;

@RestController
public class RankRuleController extends BaseActivityController implements RankRuleApi {

    @Resource
    private RankRuleService rankRuleService;
    @Resource
    private RankSubRuleService rankSubRuleService;

    @Override
    public ReturnResult rule(String language, Long rankId) {
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS),
                                    Optional.ofNullable(rankRuleService.findByRankIdAndLanguage(rankId, language))
                                        .map(RankRule::getRule)
                                        .orElse(""));
    }

    @Override
    public ReturnResult<String> subRule(String language, Long rankSubId) {
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS),
                                    Optional.ofNullable(rankSubRuleService.findByRankIdAndLanguage(rankSubId, language))
                                        .map(RankSubRule::getRule)
                                        .orElse(""));
    }

}
